<?php
use Nette\Application\UI,
    Nette\Forms\Form,
    HttpRequest,
    synopsis;

/**
 * Description of SmsPresenter
 *
 * @author buddha
 */
class SmsPresenter extends BasePresenter {

    /**
     * (non-phpDoc)
     *
     * @see Nette\Application\Presenter#startup()
     */
    protected function startup() {
        parent::startup();
    }

    public function renderDefault() {
        
    }

    protected function createComponentSms()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('username', 'sefcikj');
        $form->addHidden('password', '4522342e2a1d4e4a5c78f1ec9037dd6c');
        $form->addHidden('operatorID', '');
        $form->addHidden('api', 'povoden.com');
        $form->addText('phoneNumber', 'Telefoní číslo:')
            ->setRequired('Zadejte telefoní číslo!');
        $form->addTextArea('textsms', 'SMS:')
            ->addRule(Form::MIN_LENGTH, 'Příspěvek o méně než 3 znacích ja zakázan!', 3)
            ->addRule(Form::MAX_LENGTH, 'Příspěvek je příliš dlouhý, maximum je 160 znaků.', 160)
            ->setRequired('Zadejte text Vašeho příspěvku.');
        $form->addSubmit('send', 'Odeslat');
        
        $form->onSuccess[] = callback($this, 'smsSubmitted');
        return $form;
    }

    public static function correctCode($data) {
            $request = new HttpRequest('j.poslatsms.cz/Send', HttpRequest::METH_POST);
            $request->addQueryData($data
                    //array('kod' => '00000016')
                    );
            try {
                $request->send();
                if ($request->getResponseCode() == 200) {
                    $return="";
                }
            } catch (NS\AuthenticationException $e) {
                $form->addError($e->getMessage());
            }
        return $return;
    }
    public function smsSubmitted($form)
    {
        try {
            $data = $form->getValues();
            //$re=$this->correctCode($data);
            $this->flashMessage("Zpráva odeslána! - $re");

        } catch (NS\AuthenticationException $e) {
            $form->addError($e->getMessage());
        }
        //$this->error("neff");
        $this->redirect('Sms:');
    }
}